package com.idsmanager.zdh.service.business.portal;

import com.idsmanager.commons.utils.UUIDGenerator;
import com.idsmanager.commons.web.WebUtils;
import com.idsmanager.commons.web.context.BeanProvider;
import com.idsmanager.oauth.domain.security.SecurityUtils;
import com.idsmanager.oauth.domain.security.User;
import com.idsmanager.oauth.domain.security.UserRepository;
import com.idsmanager.oauth.service.dto.user.UserTableDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ResetUserPasswordHandler {

    private static final Logger LOG = LoggerFactory.getLogger(ResetUserPasswordHandler.class);

    private transient UserRepository userRepository = BeanProvider.getBean(UserRepository.class);

    private String uuid;

    public ResetUserPasswordHandler(String uuid) {
        this.uuid = uuid;
    }

    public UserTableDto handle(){
        User user = userRepository.findByGuid(uuid);
        if(user == null){
            LOG.warn("{}|Not found user by uuid:{}",WebUtils.getIp(),uuid);
            return new UserTableDto();
        }
        String uuid = UUIDGenerator.generate();
        user.setPassword(uuid);
        userRepository.saveUser(user);
        LOG.debug("{}|Reset user :{} password", SecurityUtils.username(),user);
        UserTableDto userTableDto = new UserTableDto(user);
        userTableDto.setPassword(uuid);
        return userTableDto;
    }
}
